package com.hpq.user.dao;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpq.common.domain.vo.request.PageBaseReq;
import com.hpq.user.domain.enums.ApplyReadStatusEnum;
import com.hpq.user.domain.enums.ApplyStatusEnum;
import com.hpq.user.domain.enums.ApplyTypeEnum;
import com.hpq.user.domain.model.UserApply;
import com.hpq.user.mapper.UserApplyMapper;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.List;

@Service
public class UserApplyDAO extends ServiceImpl<UserApplyMapper, UserApply> {
    public UserApply getApproving(Long uid, @NotNull Long targetUid) {
        return lambdaQuery()
                .eq(UserApply::getUid, uid)
                .eq(UserApply::getTargetId, targetUid)
                .eq(UserApply::getStatus, ApplyStatusEnum.WAIT_APPROVAL.getCode())
                .eq(UserApply::getType, ApplyTypeEnum.ADD_FRIEND.getCode())
                .one();
    }

    /**
     *  获取未读消息数量
     * @param targetId
     * @return
     */
    public Integer getUnReadCount(Long targetId) {
        return lambdaQuery().eq(UserApply::getTargetId, targetId)
                .eq(UserApply::getReadStatus, ApplyReadStatusEnum.UNREAD.getCode())
                .count();

    }

    /**
     *  获取好友申请列表
     * @param uid
     * @param req
     * @return
     */
    public IPage<UserApply> friendApplyPage(Long uid, PageBaseReq req) {
        return lambdaQuery()
                .eq(UserApply::getTargetId, uid)
                .eq(UserApply::getType, ApplyTypeEnum.ADD_FRIEND.getCode())
                .eq(UserApply::getStatus, ApplyStatusEnum.WAIT_APPROVAL.getCode())
                .orderByDesc(UserApply::getCreateTime)
                .page(req.plusPage());
    }

    /**
     *  批量设置已读
     * @param uid
     * @param applyIds
     */
    public void readApplies(Long uid, List<Long> applyIds) {
         lambdaUpdate()
                .eq(UserApply::getTargetId, uid)
                .eq(UserApply::getReadStatus, ApplyReadStatusEnum.UNREAD.getCode())
                .in(UserApply::getId,applyIds)
                 .eq(UserApply::getStatus, ApplyStatusEnum.WAIT_APPROVAL.getCode())
                .set(UserApply::getReadStatus, ApplyReadStatusEnum.READ.getCode())
                .update();
    }
}
